package com.showsu.editor.utils;


import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: William
 * @Description: 维护一些常量
 * @Date: Created in 11:27 上午 2023/3/14
 * @Modified By:
 */
public class Constant {

    /**
     * redis key 缓存key前缀，红薯编辑器预览笔记缓存
     */
    public static final String REDIS_KEY_CREATOR_DATASHOWS_AWEME_PREVIEW ="CDS_A_P_";
    public static final String REDIS_KEY_CREATOR_DATASHOWS_AWEME_PREVIEW_CONTENT ="CDS_A_P_C";
    public static final String CREATOR_DATASHOWS_AWEME_PREVIEW_BASE_CHAR_NUMBER = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFJHIJKLMNOPQRSTUVWXYZ";

    public static final String creatorDatashows = "红薯秀PC-";

    /**
     * OSS链接的固定前缀
     */
    public static final String showsuLinkPrefix = "showsu.oss-cn-hangzhou.aliyuncs.com";

    /**
     * xhs文件链接的固定前缀
     */
    public static final String xhsFileLinkPrefix0 = "xhscdn.com/";
    public static final String xhsFileLinkPrefix1 = "xiaohongshu.com/";

    /**
     * 处理xhs 图片转码的前缀
     */
    public static final String xhsAwemeImageTranscodeHandlePrefix = "xhsImageTranscode";

    /**
     * 处理xhs 动图的前缀
     */
    public static final String xhsAwemeAnimImgHandlePrefix = "xhsAnimImg";

    /**
     * 红薯秀编辑器图片数量上限
     */
    public static final int editorImgMaxCount = 18;
    //图片大小限制：20M
    public static final int editorImgMaxSize = 20971520;
    public static final int editorAwemeFreeMaxCount = 50;


    /**
     * 支持的域名
     */
    public static final String datashowsDomainName = ".datashows.cn";

    /**
     * 告警短信接收号码
     */
    public static final String ddMobile = "18258468394";
    /**
     * 外部接口请求最大的超时时长(毫秒)
     */
    public static final int defaultTimeOut = 120000;
    /**
     * 外部接口请求的超时时长(毫秒)，20s
     */
    public static final int defaultTimeOut_20 = 20000;
    public static final String zoreString = "0";


    /**
     * xhs链接的固定前缀
     */
    public static final String xhsLinkPrefix0 = "xhslink.com";
    public static final String xhsLinkPrefix1 = "xiaohongshu.com";
    public static final String xhsLinkPrefix2 = "xhslink.cn";
    public static List<String> xhsLinkPrefixList;


    /**
     * dy链接的固定前缀
     */
    public static final String dyLinkPrefix = "v.douyin.com";
    public static final String dyLinkPrefix1 = "www.iesdouyin.com";
    public static final String dyLinkPrefix2 = "www.douyin.com";


    /**
     * weibo链接的固定前缀
     */
    public static final String weiboLinkPrefix = "weibo.com";
    public static final String weiboLinkPrefix1 = "weibo.cn";

    /**
     * 处理xhs live实况图的前缀
     */
    public static final String xhsAwemeLiveHandlePrefix = "xhsLive";

    public static final String hsxPc = "hsxPc";

    public static final String dyAwemeLiveHandlePrefix = "dyLive";

    public static final String weiboLiveHandlePrefix = "weiboLive";

    /**
     * heic图片后缀
     */
    public static final String heicPicSuffix = ".heic";
    /**
     * 追加在阿里云oss上的heic图片url的后缀，实现
     */
    public static final String aliyunOssPicSuffix = "?x-oss-process=image/format,jpg";

    /**
     * 提示语
     */
    public static final String analyzeHit7 = "-";
    public static final String analyzeHit13 = "未知";


    public static final String cookie4SessionName = "ssets";
    public static final String versionCode = "versionCode";

    public static final String oneString = "1";

    public static final String system = "system";
    public static final String interfaceAutoSwitch = "interface_auto_switch";
    public static final String interfaceAutoTime = "interface_auto_time";
    public static final int interfaceAutoSwitchRetryTimes = 5;

    /**
     * xhs相关图片类型
     */
    public static final String CRD_WM_WEBP = "CRD_WM_WEBP";
    public static final String WB_DFT = "WB_DFT";

    public static final String xhsAwemeNotExist0 = "笔记不存在";
    public static final String xhsAwemeNotExist1 = "当前内容无法展示";
    public static final String xhsAwemeNotExist2 = "笔记被删除";
    public static final String xhsAwemeNotExist3 = "笔记正在审核中，请稍后查看";
    public static final String xhsAwemeNotExist4 = "笔记被删除or不能展示";
    public static final String xhsAwemeNotExist5 = "删除";
    public static final String xhsAwemeNotExist6 = "不能展示";
    public static final String xhsAwemeNotExist7 = "无法展示";
    public static final String xhsAwemeNotExist8 = "不存在";
    public static final String xhsAwemeNotExist9 = "访问链接异常";
    public static final String xhsAwemeNotExist10 = "Note is not available";
    public static final String analyzeHit18 = "笔记无法展示";

    public static final String xhsEditorDir = "xhsEditor";

    /**
     * xhs笔记不存在，接口返回的提示语关键字集合
     */
    public static List<String> xhsAwemeNotExistHintList;

    /**
     * xhs去水印功能支持使用的平台 白名单
     * 这个是我们业务自己定义的，为了规避不必要的风险。实际去水印接口能处理的平台很多。
     */
    public static List<String> whiteUrlList4XhsDewater;

    /**
     * xhs域名 白名单
     */
    public static List<String> whiteUrlList4Xhs;

    static {
        xhsAwemeNotExistHintList = new ArrayList<>();
        xhsAwemeNotExistHintList.add(xhsAwemeNotExist0);
        xhsAwemeNotExistHintList.add(xhsAwemeNotExist1);
        xhsAwemeNotExistHintList.add(xhsAwemeNotExist2);
        xhsAwemeNotExistHintList.add(xhsAwemeNotExist3);
        xhsAwemeNotExistHintList.add(xhsAwemeNotExist4);
        xhsAwemeNotExistHintList.add(xhsAwemeNotExist5);
        xhsAwemeNotExistHintList.add(xhsAwemeNotExist6);
        xhsAwemeNotExistHintList.add(xhsAwemeNotExist7);
        xhsAwemeNotExistHintList.add(xhsAwemeNotExist8);
        xhsAwemeNotExistHintList.add(xhsAwemeNotExist9);
        xhsAwemeNotExistHintList.add(xhsAwemeNotExist10);

        whiteUrlList4XhsDewater = new ArrayList<>();
        whiteUrlList4XhsDewater.add(xhsLinkPrefix0);
        whiteUrlList4XhsDewater.add(xhsLinkPrefix1);
        whiteUrlList4XhsDewater.add(xhsLinkPrefix2);
        whiteUrlList4XhsDewater.add(dyLinkPrefix);
        whiteUrlList4XhsDewater.add(dyLinkPrefix1);
        whiteUrlList4XhsDewater.add(dyLinkPrefix2);
        whiteUrlList4XhsDewater.add(weiboLinkPrefix);
        whiteUrlList4XhsDewater.add(weiboLinkPrefix1);

        whiteUrlList4Xhs = new ArrayList<>();
        whiteUrlList4Xhs.add(xhsLinkPrefix0);
        whiteUrlList4Xhs.add(xhsLinkPrefix1);
        whiteUrlList4Xhs.add(xhsLinkPrefix2);

        xhsLinkPrefixList = new ArrayList<>();
        xhsLinkPrefixList.add(xhsLinkPrefix0);
        xhsLinkPrefixList.add(xhsLinkPrefix1);
        xhsLinkPrefixList.add(xhsLinkPrefix2);
    }

    /**
     * 万能验证码，方便用户界面复现问题
     */
    public static final String universalIdentifyCode = "136723";

    public static final String batmkeyPrivaterRteLimiterMsg = "请求频率太快了 请稍微休息下";

    /**
     * 外部接口id
     */
    public static final Long INTERFACEID_BATMKEYPRIVATE_API_XHS_NOTE_DETAIL_4_LIVE = 65L;
    public static final Long INTERFACEID_SELFAPI_API_XHS_NOTE_DETAIL_V1 = 115L;

    /**
     * 判断文案中是否包含 xhs笔记不存在 相关的提示语关键词
     * @param msg
     * @return
     */
    public static boolean containsXhsAwemeNotExistHint(String msg) {
        if (StringUtils.isBlank(msg)) {
            return false;
        }
        for (String xhsAwemeNotExistHint : xhsAwemeNotExistHintList) {
            if (msg.indexOf(xhsAwemeNotExistHint) > -1) {
                return true;
            }
        }
        return false;
    }

    //1天
    public static int cookieMaxAge=86400;

    /**
     * 从session中获取登录验证信息
     * @param request
     * @return
     */
    public static String getSessionId(HttpServletRequest request) {
        return getCookie(request, Constant.cookie4SessionName);
    }

    private static String getCookie(HttpServletRequest request, String cookieName) {
        Cookie[] cookies = request.getCookies();
        if (null == cookies || cookies.length == 0) {
            return null;
        }
        for (Cookie cookie : cookies) {
            if (cookieName.equals(cookie.getName())) {
                cookie.setMaxAge(cookieMaxAge);
                return cookie.getValue();
            }
        }
        return null;
    }

    /**
     * 检查去水印功能传入的source是否符合白名单
     * @param source
     * @return
     */
    public static boolean checkeXhsDewaterSourceAccordWhiteUrl(String source) {
        if (StringUtils.isBlank(source)) {
            return false;
        }
        for (String whiteUrl : whiteUrlList4XhsDewater) {
            if (source.indexOf(whiteUrl) > -1) {
                return true;
            }
        }
        return false;
    }

    /**
     * 检查传入的source是否符合xhs域名白名单
     * @param source
     * @return
     */
    public static boolean checkeXhsSourceAccordWhiteUrl(String source) {
        if (StringUtils.isBlank(source)) {
            return false;
        }
        for (String whiteUrl : whiteUrlList4Xhs) {
            if (source.indexOf(whiteUrl) > -1) {
                return true;
            }
        }
        return false;
    }

    /**
     * 检查传入的source是否包含小红书官方有效域名
     * @param source
     * @return
     */
    public static boolean checkSourceContainsXhsLinkPrefix(String source) {
        if (StringUtils.isBlank(source)) {
            return false;
        }
        for (String xhsLinkPrefix : xhsLinkPrefixList) {
            if (source.contains(xhsLinkPrefix)) {
                return true;
            }
        }
        return false;
    }

}
